package com.example.bbook.controller;

import com.example.bbook.entity.UserEntity;
import com.example.bbook.services.CRUDServiceInterface;
import com.example.bbook.services.UserService;
import com.example.bbook.util.PageTool;
import com.example.bbook.util.Response;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


@CrossOrigin
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;


    @RequestMapping(value = "/login")
    public Response login(@RequestBody UserEntity user) {
        return Response.successful(userService.login(user));
    }


    @RequestMapping(value = "/register")
    public Response register(@RequestBody UserEntity user) {
        return Response.successful(userService.register(user));
    }

    @RequestMapping(value = "/info")
    public Response register(Integer id) {
        return Response.successful(userService.getUserById(id));
    }


    @RequestMapping(value = "/list")
    public Response getUserList(PageTool pageTool, UserEntity user) {
        return Response.successful(userService.findAll(pageTool, user));
    }



    @RequestMapping(value = "/add")
    public Response addUserById(@RequestBody UserEntity user) {
        Boolean addUserRes = userService.add(user);
        if (addUserRes) {
            return Response.successful();
        }
        return Response.fail();
    }

    @RequestMapping("/delete")
    public Response deleteUserById(@RequestBody Integer[] ids) {
        Boolean deleteUserRes = userService.deleteByIds(ids);
        if (deleteUserRes) {
            return Response.successful();
        }
        return Response.fail();
    }

    @RequestMapping("/update")
    public Response updateUserById(@RequestBody UserEntity user) {
        Boolean updateUserRes = userService.updateById(user);
        if (updateUserRes) {
            return Response.successful();
        }
        return Response.fail();
    }
}
